import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { OrgCodeItem } from '../models';

@Component({
  selector: 'zepride-settled-code-input',
  templateUrl: './settled-code-input.component.html',
  styleUrls: ['./settled-code-input.component.less', '../org-code-item-panel/org-code-item-panel.component.less']
})
export class SettledCodeInputComponent implements OnInit {
  visible;
  orgItem: OrgCodeItem; 
  onAddItemHandle;
  onDelOrgCodeItemHandle;
  onAddGroupItemHandle;

  constructor(
    private message: NzMessageService,
  ) { }

  ngOnInit(): void {
  }

  addAutoItem(type) {
    this.onAddItemHandle && this.onAddItemHandle(type);
  }

  addGroupItem() {
    this.onAddGroupItemHandle && this.onAddGroupItemHandle();
  }

  delItem() {
    this.onDelOrgCodeItemHandle && this.onDelOrgCodeItemHandle(this.orgItem);
  }

  plusCodeBit(item: OrgCodeItem) {
    if (item.codeBit >= 10) {
      this.message.create('error', `编码位数不能超过10位！`);
      return;
    }
    item.codeBit += 1;
  }
  minusCodeBit(item: OrgCodeItem) {
    if (item.codeBit <= 3) {
      this.message.create('error', `编码位数不能小于3位！`);
      return;
    }

    item.codeBit -= 1;
  }

  plusSeed(item: OrgCodeItem) {
    item.incrementSeed++;
  }

  minusSeed(item: OrgCodeItem) {
    if (item.incrementSeed <= 1) {
      this.message.create('error', `自增数字不能小于1！`);
      return;
    }

    item.incrementSeed--;
  }

  settledValueChange(item: OrgCodeItem) {
    let v = item.value + '';
    if (v.length > item.codeBit) {
      this.message.create('error', `已经超出当前编码位数！`);
      item.value = v.substr(0, item.codeBit);
    }
  }
  ok() {
    this.visible = false;
    this.settledValueChange(this.orgItem);
    let char = new Array(this.orgItem.codeBit - this.orgItem.value.toString().length).fill('0').join('');
    this.orgItem.value = char + this.orgItem.value.toString();
  }

}
